<?php

class Inventario extends AppModel {

    public $label = 'Inventario';
    public $useTable = 'inventario';
    public $displayField = 'id';
    public $belongsTo = array('Stock', 'Empresa', 'Delegacion');
    public $validate = array(
        'empresa_id' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'El nombre del sector es requerido'
            )
        ),
        'delegacion_id' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'El nombre de la delegación es requerido'
            )
        ),
        'stock_id' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Seleccione un producto de stock disponible'
            )
        ),
    );

    public function afterSave($created) {
        if ($created) {
            $this->query("UPDATE stock SET cantidad=cantidad-1 WHERE id='" . $this->data['Inventario']['stock_id'] . "'");
        }
    }

}